package com.qianfeng.zhangwei.sharepush.share;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.qianfeng.zhangwei.sharepush.R;

import java.util.HashMap;

import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.framework.PlatformDb;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.sina.weibo.SinaWeibo;

@ContentView(R.layout.activity_share)
public class ShareActivity extends AppCompatActivity implements PlatformActionListener {
    @ViewInject(R.id.onekeyshare_btn)
    Button onekeyshareBtn;
    @ViewInject(R.id.custom_ui_btn)
    Button customUiBtn;
    @ViewInject(R.id.advadvance_btn)
    Button advadvanceBtn;
    @ViewInject(R.id.three_login_btn)
    Button threeLoginBtn;
    @ViewInject(R.id.qq_login_img)
    TextView QQLogin;
    @ViewInject(R.id.sina_login_img)
    TextView sinaLogin;
    private BitmapUtils bitmapUtils;
    @ViewInject(R.id.head)
    private ImageView headView;
    @ViewInject(R.id.nameTv)
    private TextView nameTv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ViewUtils.inject(this);
        bitmapUtils = new BitmapUtils(this);
    }

    private void showMsg() {
        OnekeyShare oks = new OnekeyShare();
        //关闭sso认证
        oks.disableSSOWhenAuthorize();
        oks.setSilent(true);
        oks.setTitle("测试分享标题");
        oks.setText("测试分享内容");
//        oks.setImagePath("");
        oks.show(this);
    }

    @OnClick({R.id.onekeyshare_btn, R.id.custom_ui_btn, R.id.sina_login_img})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.onekeyshare_btn:
                showMsg();
                break;
            case R.id.advadvance_btn:
                //新浪微博
                Platform sinaPlatform = ShareSDK.getPlatform(this, SinaWeibo.NAME);
                showOther(sinaPlatform);
                break;

            case R.id.qq_login_img:

                break;
            case R.id.sina_login_img:
                Platform platform = ShareSDK.getPlatform(SinaWeibo.NAME);
                authorize(platform);
                break;
        }
    }

    private void showOther(Platform platform) {
        Platform.ShareParams params = new Platform.ShareParams();
        platform.share(params);
    }

    /**
     * UserName
     * PassWord
     *
     * @param plat
     */
    private void authorize(Platform plat) {
        //移除认证信息
        //判断是否已经授权认证
        if (plat.isAuthValid()) {
            //相对应品台的表
            PlatformDb db = plat.getDb();
            String userId = db.getUserId();
            String name = db.getUserName();
            String icon = db.getUserIcon();
            nameTv.setText(name);
            bitmapUtils.display(headView, icon);
            return;
        }
        //如果是没有授权认证走此处
        plat.setPlatformActionListener(this);
        plat.showUser(null);


    }

    /**
     * userName  passwrod
     * @param userId
     * @param name
     * @param imageUrl
     */
    public void login(String userId, String name, String imageUrl) {
        //去自己应用服务器认证
        new Thread(new Runnable() {
            @Override
            public void run() {

            }
        }).start();


    }


    private void showUserInfo() {

    }


    @Override
    public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
        Log.e("", hashMap.toString());
        int userId = (int) hashMap.get("id");
        String name = (String) hashMap.get("name");
        String icon = (String) hashMap.get("profile_image_url");
        nameTv.setText(name);
        bitmapUtils.display(headView, icon);
    }

    @Override
    public void onError(Platform platform, int i, Throwable throwable) {
        Log.e("", "授权失败");
    }

    @Override
    public void onCancel(Platform platform, int i) {
        Log.e("", "取消授权");
    }
}
